package com.cpi.cornpp.plantinitialcost.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.corntypes.converter.CornTypesConverter;
import com.cpi.cornpp.costelement.converter.CostElementConverter;
import com.cpi.cornpp.plantinitialcost.dto.PlantInitialCostDTO;
import com.cpi.cornpp.plantinitialcost.exception.PlantInitialCostException;
import com.cpi.cornpp.plantinitialcost.view.PlantInitialCostGroupView;
import com.cpi.cornpp.plantinitialcost.view.PlantInitialCostView;
import com.cpi.cornpp.plants.converter.PlantConverter;
import com.cs.common.utils.NumberUtil;

public class PlantInitialCostConverter {
	public List convertPlantInitialCostDTOsToViews(List plantInitialCostDTOs)throws PlantInitialCostException{
		List plantInitialCostGroupViews = null;
		Iterator iterator = null;
		int plantInitialCostDTOsSize = plantInitialCostDTOs.size();
		PlantInitialCostDTO plantInitialCostDTO = null;
		PlantInitialCostGroupView plantInitialCostGroupView = null;
		int indexPlantInitialCostGroupView = 0;
		int cornTypeIdAnt = -1;
		try{
			iterator = plantInitialCostDTOs.iterator();
			plantInitialCostGroupViews = new ArrayList();
			for( int index = 0; index < plantInitialCostDTOsSize; index++){
				plantInitialCostDTO = (PlantInitialCostDTO)iterator.next();
				plantInitialCostGroupView = convertPlantInitialCostDTOToGroupView(plantInitialCostDTO);
				indexPlantInitialCostGroupView = plantInitialCostGroupViews.indexOf(plantInitialCostGroupView);
				if( indexPlantInitialCostGroupView == -1 ){
					if( cornTypeIdAnt != -1 && cornTypeIdAnt != plantInitialCostGroupView.getCornTypesView().getCornTypeId() ){
						((PlantInitialCostGroupView) plantInitialCostGroupViews.get(plantInitialCostGroupViews.size()-1) ).setLastCornType(true);
					}
					plantInitialCostGroupViews.add(plantInitialCostGroupView);
				}else{
					plantInitialCostGroupView = (PlantInitialCostGroupView)plantInitialCostGroupViews.get(indexPlantInitialCostGroupView);
				}
				plantInitialCostGroupView.getPlantInitialCostViews().add(convertPlantInitialCostDTOToView(plantInitialCostDTO));
				cornTypeIdAnt = plantInitialCostGroupView.getCornTypesView().getCornTypeId();
			}
		}catch(PlantInitialCostException plantInitialCostException){
			throw plantInitialCostException;
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_CONVERTER, PlantInitialCostException.ACTION_LISTS);
			throw plantInitialCostException;
		}
		return plantInitialCostGroupViews;
	}

	private PlantInitialCostView convertPlantInitialCostDTOToView(PlantInitialCostDTO plantInitialCostDTO) throws PlantInitialCostException{
		PlantInitialCostView plantInitialCostView = null;
		CostElementConverter costElementConverter = null;
		try{
			plantInitialCostView = new PlantInitialCostView();
			costElementConverter = new CostElementConverter();
			plantInitialCostView.setPlantInitialCostId(plantInitialCostDTO.getPlantInitialCostId());
			plantInitialCostView.setCostElementView(costElementConverter.convert(plantInitialCostDTO.getCostElementDTO()));
			if( plantInitialCostDTO.getAmount() != 0 ){
				plantInitialCostView.setAmount(NumberUtil.convertPrice(plantInitialCostDTO.getAmount()));
			}else{
				plantInitialCostView.setAmount(StringUtils.EMPTY);
			}
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_CONVERTER, PlantInitialCostException.ACTION_LISTS);
			throw plantInitialCostException;
		}
		return plantInitialCostView;
	}

	private PlantInitialCostGroupView convertPlantInitialCostDTOToGroupView(PlantInitialCostDTO plantInitialCostDTO) throws PlantInitialCostException{
		PlantInitialCostGroupView plantInitialCostGroupView = null;
		PlantConverter plantConverter = null;
		CornTypesConverter cornTypesConverter = null;
		try{
			plantInitialCostGroupView = new PlantInitialCostGroupView();
			plantConverter = new PlantConverter();
			cornTypesConverter = new CornTypesConverter();
			plantInitialCostGroupView.setVersionId(plantInitialCostDTO.getVersionId());
			plantInitialCostGroupView.setPlantView(plantConverter.converterPlantDtoToView(plantInitialCostDTO.getPlantDTO()));
			plantInitialCostGroupView.setCornTypesView(cornTypesConverter.converterCornTypesDtoToView(plantInitialCostDTO.getCornTypesDTO()));
			plantInitialCostGroupView.setUnloadAmount(NumberUtil.convertTon(plantInitialCostDTO.getUnloadAmount()));
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_CONVERTER, PlantInitialCostException.ACTION_LISTS);
			throw plantInitialCostException;
		}
		return plantInitialCostGroupView;
	}
	
	public PlantInitialCostDTO convertPlantInitialCostViewToDTO(PlantInitialCostView plantInitialCostView) throws PlantInitialCostException{
		PlantInitialCostDTO plantInitialCostDTO = null;
		try{
			plantInitialCostDTO = new PlantInitialCostDTO();
			plantInitialCostDTO.setPlantInitialCostId(plantInitialCostView.getPlantInitialCostId());
			if( !StringUtils.isEmpty(plantInitialCostView.getAmount()) ){
				plantInitialCostDTO.setAmount(NumberUtil.parseDouble(plantInitialCostView.getAmount()));
			}else{
				plantInitialCostDTO.setAmount(0);
			}
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_CONVERTER, PlantInitialCostException.ACTION_UPDATE);
			throw plantInitialCostException;
		}
		return plantInitialCostDTO;
	}
}